"""

放一些公共的函数(工具函数)
"""
import functools

from flask import current_app
from flask import g
from flask import session

from info.models import User


def do_class_name(num): # num用于接收 | 前面的 loop.index

    if num == 1:
        return "first"
    elif num == 2:
        return "second"
    elif num == 3:
        return "third"
    else:
        return ""


def get_login_data(view_func):  # 书写装饰器，来获取用户登录信息
    @functools.wraps(view_func)
    def wrapper(*args, **kwargs):
        # 1.1 拿数据session的user_id
        user_id = session.get("user_id")
        # 1.2查询数据库 为了拿到用户的相关数据
        user = None
        try:
            user = User.query.get(user_id)
        except Exception as e:
            current_app.logger.error(e)

        g.user = user
        return view_func(*args, **kwargs)
    return wrapper